package com.jl.card.stk;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.annotations.JSConstructor;
import org.mozilla.javascript.annotations.JSFunction;
import org.mozilla.javascript.annotations.JSGetter;

import com.jl.card.Card;
import com.jl.card.ICard;
import com.jl.card.ISTK;
import com.jl.card.CardJSProxy;
import com.jl.card.provider.jl2g.STKUIControl;

public class STKJSProxy extends ScriptableObject implements ISTK {

	private ISTK stk;

	public STKJSProxy() {
	}

	@JSConstructor
	public STKJSProxy(Object obj) {
		Object jsCard = Context.jsToJava(obj, ICard.class);
		Card card = null;
		if (jsCard instanceof Card) {
			card = (Card) jsCard;
		} else if (jsCard instanceof CardJSProxy) {
			card = ((CardJSProxy) jsCard).getInnerCard();
		}else{
			throw new RuntimeException("parameter one must be instance of Card");
		}
		stk = STKManager.getInstance().getSTK(card);
	}

	@Override
	@JSFunction
	public void on() {
		stk.on();
	}

	@Override
	@JSFunction
	public void off() {
		stk.off();
	}

	@Override
	@JSFunction
	public void select(int index) {
		stk.select(index);
	}

	@Override
	@JSFunction
	public void ok() {
		stk.ok();
	}

	@Override
	@JSFunction
	public void back() {
		stk.back();
	}

	@Override
	@JSFunction
	public void input(String data) {
		stk.input(data);
	}

	@Override
	@JSFunction
	public String show() {
		return stk.show();
	}

	@Override
	public String getClassName() {
		return "STK";
	}

	@Override
	@JSFunction
	public void envelop(String data) {
		stk.envelop(data);
	}

	@JSGetter
	public int state() {
		return stk.getUI().getType();
	}

	@Override
	@JSGetter
	public STKUIControl getUI() {
		return stk.getUI();
	}

	@Override
	@JSFunction
	public void selectByText(String text) {
		stk.selectByText(text);
	}

}
